package com.feihao.zhou.UDP.demo01Test;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class ReceiveDemo {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(10086);

        while (true) {
            //
            byte[] bytes = new byte[1024];
            DatagramPacket packet = new DatagramPacket(bytes,bytes.length);

            socket.receive(packet);

            byte[] data = packet.getData();
            System.out.println(new String(data,0,data.length));
        }
    }
}


/*
*  //创建接收对象：指定接收端口：
        DatagramSocket ds = new DatagramSocket(10000);

        while (true) {
            //创建箱子接收：
            byte[] bytes = new byte[1024];
            DatagramPacket dp = new DatagramPacket(bytes,bytes.length);


            //通过socket对象接收打包好的东西
            ds.receive(dp);

            //获取包内东西：
            byte[] data = dp.getData();
            //打印内容：
            System.out.println(new String(data,0,data.length));

        }
    }*/